package com.jiangchh.order_service.controller;

import com.jiangchh.order_service.service.ProductOrderService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("api/v1/order")
@RefreshScope
public class OrderController {

    @Autowired
    private ProductOrderService orderService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("save")
    @HystrixCommand(fallbackMethod = "saveOrderFail")
    public Object save(@RequestParam("user_id")int userId, @RequestParam("product_id")int productId, HttpServletRequest request){

        String token = request.getHeader("token");
        String cookie = request.getHeader("cookie");

        System.out.println("token:"+token);
        System.out.println("coolie:"+cookie);

        Map<String, Object> data = new HashMap<>();
        data.put("code",0);
        data.put("data",orderService.save2(userId,productId));
        return data;
    }

    //注意，方法签名一定要和api方法一致
    private Object saveOrderFail(int userId, int productId, HttpServletRequest request){
        final String ip = request.getRemoteAddr();
        //监控报警
        /*String saveOrderKey = "save-order";
        String sendValue = stringRedisTemplate.opsForValue().get(saveOrderKey);

        new Thread( ()->{
            if(StringUtils.isBlank(sendValue)){
                System.out.println("紧急短信，用户下单失败，请立即查找原因,ip是"+ip);
                //发送一个http请求，调用短信服务 TODO

                stringRedisTemplate.opsForValue().set(saveOrderKey,"save-order-fail",20, TimeUnit.SECONDS);
            }else{
                System.out.println("已经发送过短信，20秒内不重复发送");
            }
        }).start();*/

        Map<String, Object> msg = new HashMap<>();
        msg.put("code",-1);
        msg.put("msg","抢购人数太多，您被挤出来了，稍后重试");
        return msg;
    }
}
